package com.example.timetable.fragments;


import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.timetable.R;
import com.example.timetable.TimeTableCreate;
import com.example.timetable.method.Database;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;


public class TimeTableMain extends Fragment {

    String [] spinnerItems={"第1周","第2周","第3周","第4周","第5周","第6周",
            "第7周","第8周","第9周","第10周","第11周","第12周",
            "第13周","第14周","第15周","第16周","第17周","第18周","第19周","第20周"};
    ArrayList<Map<String,Object>> arrayList;
    @BindView(R.id.switchWeek)
    Spinner weekSpinner;
    @BindView(R.id.year)
    TextView text_year;
    private ArrayAdapter<String> adapter;
    Cursor[] cursor = new Cursor[7];
    TextView textView;
    TextView textview;
    View myView;
    Context context ;
    int count;
    int spinner_position;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_time_table_main, container, false);
        ButterKnife.bind(this, view);

        //下拉列表（周数选择）
        adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_dropdown_item_1line,spinnerItems);

        weekSpinner.setAdapter(adapter);
        context = view.getContext();
        myView = view;
        Calendar calender=Calendar.getInstance();
        text_year.setText(String.valueOf(calender.get(Calendar.YEAR)));
        return view;
    }


    @OnItemSelected(R.id.switchWeek)
    public void onCLickSwichWeek(AdapterView<?> parent,int position){
        System.out.println("你选择的是："+adapter.getItem((position)));

        spinner_position=position;
        //页面信息展示
        Database db = new Database(getActivity());
        for(int i=0;i<7;i++){
            cursor[i]=db.select(i);
        }
        for(int i=0;i<7;i++)
        {
            if(cursor[i].getCount()!=0)
            {
                //               count=0;    //记录当前表中课程数量
                while (cursor[i].moveToNext())
                {
                    //                  count++;
                    //根据索引值获取当前行数所保存的课程的数据
                    String course_name = cursor[i].getString(cursor[i].getColumnIndex("classes"));
                    String course_place = cursor[i].getString(cursor[i].getColumnIndex("location"));
                    String course_weekS = cursor[i].getString(cursor[i].getColumnIndex("zhoushus"));
                    String course_weekE = cursor[i].getString(cursor[i].getColumnIndex("zhoushue"));
                    String course_timeS = cursor[i].getString(cursor[i].getColumnIndex("jieshus"));
                    String course_timeE = cursor[i].getString(cursor[i].getColumnIndex("jieshue"));
                    String course_weekW = cursor[i].getString(cursor[i].getColumnIndex("which"));
                    String course_color = cursor[i].getString(cursor[i].getColumnIndex("color"));


                    System.out.println(course_name);
                    System.out.println(course_place);
                    System.out.println(course_timeS);
                    System.out.println(course_weekW);
                    System.out.println(course_color);
                    //找到具体的id 由星期+开始节数代表的id
                    String id = "btn"+(i+1)+"_"+(change_StringToInt(course_timeS)/2+1);
//                    int color_id=context.getResources().getIdentifier(course_color,"color","com.example.timetable");
                    int view_id = context.getResources().getIdentifier(id,"id","com.example.timetable");
                    System.out.println(id);
                    System.out.println(view_id);
                    System.out.println(course_color);
                    textview = myView.findViewById(view_id);

                    //判断周数是否满足条件，即  开始周<当前周<结束周
                    if(change_StringToInt( adapter.getItem(position))<= change_StringToInt(course_weekE)
                            && change_StringToInt(adapter.getItem(position))>=change_StringToInt(course_weekS))
                    {
                        System.out.println("周数满足");
                        if(change_StringToInt(course_timeS)%2==1)     //课程时间满足条件时起始时间满足1 3 5 7 9 11
                        {
                            System.out.println("上课时间满足");
                            if(change_StringToInt(course_weekW)==1)   //单周
                            {
                                System.out.println("单周");
                                if(change_StringToInt(adapter.getItem(position))%2==1)
                                {
                                    textview.setText(course_name+"\n"+course_place+"\n"
                                            + change_StringToInt(course_weekS)+"-"+change_StringToInt(course_weekE));
                                    //格式为：课程名
                                    //       上课地点
                                    //     第几周到第几周
//                                    textView.setBackgroundColor(color_id);
                                    textview.setBackgroundColor(Integer.parseInt(course_color));
                                }
                                else textview.setText("");
                            }
                            if(change_StringToInt(course_weekW)==2)   //双周
                            {
                                System.out.println("双周");
                                if(change_StringToInt(adapter.getItem(position))%2==0)
                                {
                                    textview.setText(course_name+"\n"+course_place+"\n"
                                            + change_StringToInt(course_weekS)+"-"+change_StringToInt(course_weekE));
                                    textview.setBackgroundColor(Integer.parseInt(course_color));
//                                    textView.setBackgroundColor(color_id);
                                }
                                else textview.setText("");
                            }
                            if(change_StringToInt(course_weekW)==3)    //连续周
                            {
                                System.out.println("连续周");

                                textview.setText(course_name+"\n"+course_place+"\n"
                                        + change_StringToInt(course_weekS)+"-"+change_StringToInt(course_weekE));
                                //                               textView.setBackgroundColor(color_id);
                                textview.setBackgroundColor(Integer.parseInt(course_color));
                            }
                        }
                        else
                        {
                            textview.setText("");
                            textview.setBackgroundColor(Color.WHITE);
                        }
                    }
                    else
                    {
                        textview.setText("");
                        textview.setBackgroundColor(Color.WHITE);
                    }
                }
                System.out.println("count="+count);
                cursor[i].close();
            }

        }
        parent.setVisibility(View.VISIBLE);
    }

    //点击课程进入课程创建页面并传入信息
    @OnClick({R.id.btn1_1,R.id.btn1_2,R.id.btn1_3,R.id.btn1_4,R.id.btn1_5,R.id.btn1_6,
            R.id.btn2_1,R.id.btn2_2,R.id.btn2_3,R.id.btn2_4,R.id.btn2_5,R.id.btn2_6,
            R.id.btn3_1,R.id.btn3_2,R.id.btn3_3,R.id.btn3_4,R.id.btn3_5,R.id.btn3_6,
            R.id.btn4_1,R.id.btn4_2,R.id.btn4_3,R.id.btn4_4,R.id.btn4_5,R.id.btn4_6,
            R.id.btn5_1,R.id.btn5_2,R.id.btn5_3,R.id.btn5_4,R.id.btn5_5,R.id.btn5_6,
            R.id.btn6_1,R.id.btn6_2,R.id.btn6_3,R.id.btn6_4,R.id.btn6_5,R.id.btn6_6,
            R.id.btn7_1,R.id.btn7_2,R.id.btn7_3,R.id.btn7_4,R.id.btn7_5,R.id.btn7_6})
    public void Clicked(View v){
        //跳转到创建界面
        String textviewId =getResources().getResourceEntryName(v.getId());
        System.out.println(textviewId);
        System.out.println(Integer.parseInt(textviewId.substring(3,4)));
        System.out.println(Integer.parseInt(textviewId.substring(5,6))*2-1);
        textView=myView.findViewById(v.getId());
        if(textView.getText().length()==0)          //未编辑课程
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(),TimeTableCreate.class);
            startActivity(intent);
        }
        else                                        //已编辑课程
        {
            Intent intent = new Intent();
            intent.setClass(getActivity(),TimeTableCreate.class);
            intent.putExtra("week",textviewId.substring(3,4));    //星期
            intent.putExtra("jies",textviewId.substring(5,6));  //开始节数
            startActivity(intent);
        }
    }

    //字符串转换
    public int change_StringToInt(String s)
    {
        if(s.equals("第1周") || s.equals("第一节") || s.equals("单周"))
        {
            return 1;
        }
        if(s.equals("第2周") || s.equals("第二节") || s.equals("双周"))
        {
            return 2;
        }
        if(s.equals("第3周") || s.equals("第三节") || s.equals("连续周"))
        {
            return 3;
        }
        if(s.equals("第4周") || s.equals("第四节"))
        {
            return 4;
        }
        if(s.equals("第5周") || s.equals("第五节"))
        {
            return 5;
        }
        if(s.equals("第6周") || s.equals("第六节"))
        {
            return 6;
        }
        if(s.equals("第7周") || s.equals("第七节"))
        {
            return 7;
        }
        if(s.equals("第8周") || s.equals("第八节"))
        {
            return 8;
        }
        if(s.equals("第9周") || s.equals("第九节"))
        {
            return 9;
        }
        if(s.equals("第10周") || s.equals("第十节"))
        {
            return 10;
        }
        if(s.equals("第11周") || s.equals("第十一节"))
        {
            return 11;
        }
        if(s.equals("第12周") || s.equals("第十二节"))
        {
            return 12;
        }
        if(s.equals("第13周") || s.equals("第十三节"))
        {
            return 13;
        }
        if(s.equals("第14周"))
        {
            return 14;
        }
        if(s.equals("第15周"))
        {
            return 15;
        }
        if(s.equals("第16周"))
        {
            return 16;
        }
        if(s.equals("第17周"))
        {
            return 17;
        }
        if(s.equals("第18周"))
        {
            return 18;
        }
        if(s.equals("第19周"))
        {
            return 19;
        }
        if(s.equals("第20周"))
        {
            return 20;
        }
        if(s.equals("上午\n1-2节"))
        {
            return 1;
        }
        if(s.equals("上午\n3-4节"))
        {
            return 2;
        }
        if(s.equals("中午\n5-6节"))
        {
            return 3;
        }
        if(s.equals("下午\n7-8节"))
        {
            return 4;
        }
        if(s.equals("下午\n9-10节"))
        {
            return 5;
        }
        if(s.equals("晚上\n11-13节"))
        {
            return 6;
        }
        else
        {
            return 0;
        }
    }
}

